#!/usr/bin/env python
import sys,os

def istextfile(path):
    if 'text' in os.popen('file "%s"'%path).read():
        return True
    return False

def parsedir(arg,path,fnames):
    f = file('dirinfo.log','a')
    f.write(path+'\n')
    f.close()
    res = arg[1]
    #remove svn dir
    if type(arg[0]) is list:
        for dir in arg[0]:
            if dir in fnames:
                fnames.remove(dir)
    for name in fnames:
        if name.endswith('~'):
            #no backup files
            continue
        fullpath = os.path.join(path,name)
        if os.path.isfile(fullpath):
            if istextfile(fullpath):
                res[0] += len(file(fullpath).readlines())
                res[3] += 1
            res[1] += os.stat(fullpath).st_size
            res[2] += 1
            if res[2] % 3000 == 0:
                print '# files: %dk, path: %s' % (res[2]/1000,fullpath)

def main():
    if '-r' in sys.argv[1:-1]:
        i = sys.argv.index('-r')
        rdirs = sys.argv[i+1].split(',')
        del sys.argv[i+1]
        del sys.argv[i]
    else:
        rdirs = None
    if sys.argv[1:]:
        basepath = sys.argv[1]
    else:
        basepath = '.'

    res = [0] * 4 #num lines, bytes count, files count, text files count
    try:
        os.path.walk(basepath,parsedir,(rdirs,res))
    except KeyboardInterrupt:
        pass
    print '# lines',res[0]
    print 'bytes count',res[1]
    print 'files count',res[2]
    print 'text files count',res[3]

if __name__=="__main__":
    main()
